
import axios from 'axios'
import {MessageBox} from 'element-ui';
var urlbaseV = '';
// if(process.env.NODE_ENV == 'production'){//生产
//   if(window.location.host.split(':')[1] == '8090'){
//     axios.defaults.baseURL = 'http://www.yunchejinrong.com:8090/api/v1'//测试8090
//   }else{
//     axios.defaults.baseURL = 'http://www.yunchejinrong.com:8081/api/v1'//正式
//   }
// }else{//开发
//   axios.defaults.baseURL = '/adminApi/api/v1'
// }
axios.defaults.baseURL = '/adminApi'
//
axios.defaults.withCredentials = true
// 请求时的拦截
axios.interceptors.request.use(config => {
  // 发送请求之前做一些处理
  // config.headers = {
  //   "Access-Control-Allow-Origin": "*",
  //   "Content-Type": "application/json;charset=UTF-8",
  // }
  return config
}, error => {
  // 当请求异常时做一些处理
  return Promise.reject(error)
})

// 响应时拦截
axios.interceptors.response.use(response => {
  // 返回响应时做一些处理
  return response
}, error => {
  // 当响应异常时做一些处理
  return Promise.resolve(error)
})

function checkStatus(response) {  
  if (response && response.status === 304) {
    return {
      code: 304,
      success: false,
      msg: response.data.msg
    }
  } else if (response && response.status === 400) {
    return {
      code: 400,
      success: false,
      msg: response.data.msg
    }
  } else if (response && response.status === 500) { 
    return {
      code: 500,
      success: false,
      msg: response.data.msg
    }
  }else if (response && response.status === 200) { 
    return response.data
  }else if (response && response.code === 500){
    return {
      code: 500,
      success: false,
      msg: response.msg
    }
  }
  return {
    code: -404,
    success: false,
    msg: response.data.msg
  }
}

function getCookie(name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  if (arr = document.cookie.match(reg))
    return (arr[2]);
  else
    return null;
}

function checkCode(res) { 
  // 如果code异常(这里已经包括网络错误，服务器错误，后端抛出的错误)，可以弹出一个错误提示，告诉用户
  window.loginPage = '/#/login'
  if (res.success) {
    return res
  } else {
    if (res.code == '-3') {
      MessageBox(res.msg,()=>{
        window.location = window.loginPage
        return {success:false};
      })
      return {success:false};
    }else{
     if(res.msg){
       MessageBox(res.msg,() => {

        })
      }else{
       MessageBox('网络异常，请稍后再试',() => {

       })
      } 
    } 
    return {success:false};
  }
}

export default {
  post(url, data) {
    return axios({
      method: 'post',
      url,
      withCredentials: true,
      dataType: 'json',
      data: JSON.stringify(data),
      timeout: 50000,
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json;charset=UTF-8",
      }
    }).then(
      (response) => { 
        return checkStatus(response)
      }
    ).then(
      (res) => { 
        return checkCode(res);
      }
    );
  },
  get(url, params) {
    return axios({
      method: 'get',
      url,
      params,
      timeout: 50000,
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
      }
    }).then(
      (response) => {
        return checkStatus(response)
      }
    ).then(
      (res) => {
        return checkCode(res)
      }
    );
  },

}
